#!/usr/bin/env python

# encoding: utf-8

'''
 * Create File __init__.py
 * Created by leixu on 2017/10/16
 * IDE PyCharm
'''
from selenium import webdriver
import os


def custom_chrome_config(folder=None, is_headless=False, http_proxy=None):
    """

    :param folder:
        where the user data to put
        default is None
    :param is_headless:
        is use headless mode
    :param http_proxy:
        if use http_proxy
    :return:
        dict config
    """
    config = {}
    options = webdriver.ChromeOptions()
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    desired_capabilities = DesiredCapabilities().CHROME.copy()
    options.add_argument('--headless') if is_headless else 1
    options.add_argument('window-size=1920x1080')
    options.add_argument(f"--user-data-dir=.\\{folder}") if folder else 1
    options.add_argument(f"--disable-gpu")
    prefs = {"profile.managed_default_content_settings.images": 2,
             "credentials_enable_service": False,
             "profile.default_content_setting_values.notifications": 2}
    options.add_experimental_option("prefs", prefs)
    options.add_argument(f'--proxy-server=http://{http_proxy}') if http_proxy else 1
    s = os.path.abspath(os.path.join(os.path.realpath(__file__), "../../../crx_dir"))
    options.add_extension(os.path.join(s, "fb_adblock_1.2.22_0.crx"))
    options.add_argument("--disable-notifications")
    config["chrome_options"] = options
    config["desired_capabilities"] = desired_capabilities
    return config
